package com.workcell.workdata.xthings.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.common.excel.annotation.RequestExcel;
import com.workcell.workdata.common.excel.annotation.ResponseExcel;
import com.workcell.workdata.common.security.annotation.Inner;
import com.workcell.workdata.xthings.dto.CodeChainnumUpdateAliasVo;
import com.workcell.workdata.xthings.dto.CodeChainnumVo;
import com.workcell.workdata.xthings.dto.ExcelImportTemplateVo;
import com.workcell.workdata.xthings.entity.CodeChainnum;
import com.workcell.workdata.xthings.entity.CodeLevelGauge;
import com.workcell.workdata.xthings.entity.CodeTimeClock;
import com.workcell.workdata.xthings.service.CodeChainnumService;
import com.workcell.workdata.xthings.service.CodeExcelService;
import com.workcell.workdata.xthings.service.CodeLevelGaugeService;
import com.workcell.workdata.xthings.service.CodeTimeClockService;
import com.workcell.workdata.xthings.service.factory.CodeBaseFactory;
import com.workcell.workdata.xthings.utils.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 二维码码库
 *
 * @author 廖博文
 * @date 2022-11-30 09:34:00
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/codechainnum")
@Api(value = "codechainnum", tags = "二维码码库管理")
public class CodeChainnumController {

    private final CodeChainnumService codeChainnumService;
    private final CodeExcelService codeExcelService;
    private final CodeBaseFactory codeBaseFactory;

    private final CodeLevelGaugeService codeLevelGaugeService;

    private final CodeTimeClockService codeTimeClockService;

    /**
     * 分页查询
     *
     * @param page         分页对象
     * @param codeChainnum 二维码码库
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getCodeChainnumPage(Page page, CodeChainnum codeChainnum) {
        return R.ok(codeChainnumService.page(page, Wrappers.query(codeChainnum)));
    }

    /**
     * 模糊分页查询
     *
     * @param selectChainNumVo
     * @return
     */
    @ApiOperation(value = "模糊分页查询", notes = "模糊分页查询")
    @GetMapping("/vaguePage")
    public R getCodeChainnumVaguePage(CodeChainnumVo.SelectChainNumVo selectChainNumVo) {
        return R.ok(codeChainnumService.getCodeChainnumVaguePage(selectChainNumVo));
    }

    /**
     * 通过类型查找硬件清单
     *
     * @param type id
     * @return R
     */
    @ApiOperation(value = "通过类型查找硬件清单", notes = "通过类型查找硬件清单")
    @GetMapping("/getHardewareList")
    public R getHardewareList(String type) {
        return R.ok(codeChainnumService.getHardewareList(type));
    }

    /**
     * 查询硬件快照信息
     *
     * @return R
     */
    @ApiOperation(value = "查询硬件快照信息", notes = "查询硬件快照信息")
    @GetMapping("/getHardwareInfo")
    public R getHardwareInfo(String codeId, String type) {
        Object resultBean = codeChainnumService.getHardwareInfo(codeId, type);
        if (ObjectUtil.isEmpty(resultBean)) {
            return R.failed("查询数据为空");
        }
        return R.ok(resultBean);
    }

    /**
     * 查询硬件基本信息
     *
     * @param baseId
     * @param type
     * @return
     */
    @ApiOperation(value = "查询硬件基本信息", notes = "查询硬件基本信息")
    @GetMapping("/getHardwareBase")
    public R getHardwareBase(String baseId, String type) {
        Object resultBean = codeBaseFactory.getHardwareBase(baseId, type);
        if (ObjectUtil.isEmpty(resultBean)) {
            return R.failed("查询数据为空");
        }
        return R.ok(resultBean);
    }

    /**
     * 查询二维码地址
     *
     * @return R
     */
    @ApiOperation(value = "查询二维码地址", notes = "查询二维码地址")
    @PostMapping("/getQrCodeById")
    public R getQrCodeById(@RequestBody List<String> ids) {
        return R.ok(codeChainnumService.getQrCodeById(ids));
    }

    /**
     * 通过id查询二维码码库
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id) {
        return R.ok(codeChainnumService.getById(id));
    }

    /**
     * 新增二维码码库
     *
     * @param saveChainNumVo 二维码码库
     * @return R
     */
    @ApiOperation(value = "新增二维码码库", notes = "新增二维码码库")
    @PostMapping
    public R save(@RequestBody @Validated CodeChainnumVo.SaveChainNumVo saveChainNumVo) {
        try {
            return R.ok(codeChainnumService.saveCodeChainNumInfo(saveChainNumVo));
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
    }

    /**
     * 修改二维码码库
     *
     * @param codeChainnum 二维码码库
     * @return R
     */
    @ApiOperation(value = "修改二维码码库", notes = "修改二维码码库")
    @PutMapping
    public R updateById(@RequestBody CodeChainnum codeChainnum) {
        return R.ok(codeChainnumService.updateById(codeChainnum));
    }

    /**
     * 通过id删除二维码码库
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除二维码码库", notes = "通过id删除二维码码库")
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable String id) {
        try {
            return codeChainnumService.removeCodeChainNumById(id);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
    }


    /**
     * 导出excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @ApiOperation(value = "导出excel模板", notes = "导出excel模板")
    @PostMapping("/exportTemplate")
    public List<Object> exportTemplate(@RequestBody @Validated CodeChainnumVo.ExcelExportChainNumVo excelExportChainNumVo) {
        return ObjectUtils.castList(codeExcelService.exportTemplate(excelExportChainNumVo.getBaseId()
                , excelExportChainNumVo.getHardwareType(), excelExportChainNumVo.getNum()), Object.class);
    }

    /**
     * 导入流量计二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入流量计二维码信息", notes = "导入流量计二维码信息")
    @PostMapping("/importFlowMeterCodeChainnum")
    public R importFlowMeterCodeChainnum(@RequestExcel List<ExcelImportTemplateVo.FlowMeterVo> excelVOList) {
        return codeExcelService.importFlowMeterCodeChainnum(excelVOList);
    }

    /**
     * 导入功率计二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入智能电表二维码信息", notes = "导入智能电表二维码信息")
    @PostMapping("/importPowerMeterCodeChainnum")
    public R importPowerMeterCodeChainnum(@RequestExcel List<ExcelImportTemplateVo.PowerMeterVo> excelVOList) {
        return codeExcelService.importPowerMeterCodeChainnum(excelVOList);
    }

    /**
     * 导入串口设备二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入串口设备二维码信息", notes = "导入串口设备二维码信息")
    @PostMapping("/importSerialDevicerCodeChainnum")
    public R importSerialDevicerCodeChainnum(@RequestExcel List<ExcelImportTemplateVo.SerialDeviceVo> excelVOList) {
        return codeExcelService.importSerialDeviceCodeChainnum(excelVOList);
    }

    /**
     * 导入电视盒子二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入电视盒子二维码信息", notes = "导入电视盒子二维码信息")
    @PostMapping("/importTvBoxCodeChainnum")
    public R importTvBoxCodeChainnum(@RequestExcel List<ExcelImportTemplateVo.TvBoxVo> excelVOList) {
        return codeExcelService.importTvBoxCodeChainnum(excelVOList);
    }

    /**
     * 导入打卡机二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入打卡机二维码信息", notes = "导入打卡机二维码信息")
    @PostMapping("/importTimeClockCodeChainnum")
    public R importTimeClockCodeChainnum(@RequestExcel List<ExcelImportTemplateVo.TimeClockVo> excelVOList) {
        return codeExcelService.importTimeClockCodeChainnum(excelVOList);
    }

    /**
     * 导入摄像头二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入摄像头二维码信息", notes = "导入摄像头二维码信息")
    @PostMapping("/importCameraCodeChainnum")
    public R importCameraCodeChainnum(@RequestExcel List<ExcelImportTemplateVo.CameraVo> excelVOList) {
        return codeExcelService.importCameraCodeChainnum(excelVOList);
    }

    /**
     * 导入皮带秤二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入皮带秤二维码信息", notes = "导入皮带秤二维码信息")
    @PostMapping("/importBeltScalesCodeChainnum")
    public R importBeltScalesCodeChainnum(@RequestExcel List<ExcelImportTemplateVo.BeltScales> excelVOList) {
        return codeExcelService.importBeltScalesCodeChainnum(excelVOList);
    }

    /**
     * 导入温湿度计二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入温湿度计二维码信息", notes = "导入温湿度计二维码信息")
    @PostMapping("/importTemperatureHumidityCodeChainnum")
    public R importTemperatureHumidityCodeChainnum(@RequestExcel List<ExcelImportTemplateVo.TemperatureHumidityVo> excelVOList) {
        return codeExcelService.importTemperatureHumidityCodeChainnum(excelVOList);
    }

    /**
     * 导入冷冻机二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入冷冻机二维码信息", notes = "导入冷冻机二维码信息")
    @PostMapping("/importFreezerCodeChainnum")
    public R importFreezerCodeChainnum(@RequestExcel List<ExcelImportTemplateVo.Freezer> excelVOList) {
        return codeExcelService.importFreezerCodeChainnum(excelVOList);
    }

    /**
     * 导入换热机二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入换热机二维码信息", notes = "导入换热机二维码信息")
    @PostMapping("/importHeatExchangersCodeChainnum")
    public R importHeatExchangersCodeChainnum(@RequestExcel List<ExcelImportTemplateVo.HeatExchangers> excelVOList) {
        return codeExcelService.importHeatExchangersCodeChainnum(excelVOList);
    }

    @ApiOperation("查看设备信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "设备编号", name = "deviceNo", dataTypeClass = String.class),
            @ApiImplicitParam(value = "设备类型", name = "hardwareType", dataTypeClass = String.class)
    })
    @GetMapping("/deviceDetail")
    public R deviceDetail(String deviceNo, String hardwareType) {
        return R.ok(codeChainnumService.deviceDetail(deviceNo, hardwareType));
    }

    @Inner
    @ApiOperation("根据短链码和设备类型查询")
    @GetMapping("/getByChainnumAndHardwareType")
    public R<CodeChainnum> getByChainnumAndHardwareType(String chainNum, String hardwareType) {
        return R.ok(codeChainnumService.getByChainnumAndHardwareType(chainNum, hardwareType));
    }

    @ApiOperation(value = "导入阀门二维码信息", notes = "导入阀门二维码信息")
    @PostMapping("/importValveChainnum")
    public R importValveChainnum(@RequestExcel List<ExcelImportTemplateVo.ValveVo> excelVOList) {
        return codeExcelService.importValveChainnum(excelVOList);
    }

    @Inner
    @ApiOperation("根据厂家设备编号查找厂商编码")
    @GetMapping("/getByDeviceNo")
    public R<CodeChainnum> getByDeviceNo(String deviceNo, String hardwareType) {
        return R.ok(codeChainnumService.getByDeviceNo(deviceNo, hardwareType));
    }

    /**
     * 导入液位计二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入液位计二维码信息", notes = "导入液位计二维码信息")
    @PostMapping("/importLeveGaugeChainnum")
    public R importLeveGaugeChainnum(@RequestExcel List<ExcelImportTemplateVo.LeveGaugeClockVo> excelVOList) {
        return codeExcelService.importLeveGaugeChainnum(excelVOList);
    }

    /**
     * 导入天然气二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入天然气二维码信息", notes = "导入天然气二维码信息")
    @PostMapping("/importNaturalGasChainnum")
    public R importNaturalGasChainnum(@RequestExcel List<ExcelImportTemplateVo.NaturalGasVo> excelVOList) {
        return codeExcelService.importNaturalGasChainnum(excelVOList);
    }

    @ApiOperation(value = "根据设备编码修改设备备注名称")
    @PostMapping("updateAliasByDeviceNo")
    public R<Boolean> updateAliasByDeviceNo(@RequestBody @Valid CodeChainnumUpdateAliasVo codeChainnumUpdateAliasVo) {
        return R.ok(codeChainnumService.updateAliasByDeviceNo(codeChainnumUpdateAliasVo));
    }

    @Inner
    @ApiOperation(value = "查询液位计快照信息", notes = "查询液位计快照信息")
    @GetMapping("/getCodeLevelGaugeInfo")
    public R<CodeLevelGauge> getCodeLevelGaugeInfo(String codeId) {
        CodeLevelGauge resultBean = codeChainnumService.getCodeLevelGaugeInfo(codeId);
        if (ObjectUtil.isEmpty(resultBean)) {
            return R.failed("查询数据为空");
        }
        return R.ok(resultBean);
    }

    @Inner
    @ApiOperation(value = "根据设备编码修改设备快照状态")
    @PutMapping("updateHardwareStatus")
    public R<Boolean> updateHardwareStatus(String deviceNo, String manufacturerCode, String status, String hardwareType) {
        return R.ok(codeChainnumService.updateHardwareStatus(deviceNo, manufacturerCode, status, hardwareType));
    }

    @Inner
    @ApiOperation(value = "查询硬件快照信息", notes = "查询硬件快照信息")
    @GetMapping("/getHardwareBaseOpen")
    public R getHardwareBaseOpen(String deviceNo, String hardwareType) {
        Object resultBean = codeBaseFactory.getHardwareBaseOpen(deviceNo, hardwareType);
        if (ObjectUtil.isEmpty(resultBean)) {
            return R.failed("查询数据为空");
        }
        return R.ok(resultBean);
    }

    /**
     * 导入上位机二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入上位机二维码信息", notes = "导入上位机二维码信息")
    @PostMapping("/importUpperComputerChainnum")
    public R importUpperComputerChainnum(@RequestExcel List<ExcelImportTemplateVo.UpperComputerVo> excelVOList) {
        return codeExcelService.importUpperComputerChainnum(excelVOList);
    }

    /**
     * 查找液位计型号
     */
    @Inner
    @ApiOperation(value = "查找液位计型号", notes = "查找液位计型号")
    @GetMapping("/getLevelGaugeModel")
    public R<CodeLevelGauge> getLevelGaugeModel(String deviceNo) {
        return R.ok(codeLevelGaugeService.getLevelGaugeModel(deviceNo));
    }

    /**
     * 导入智能罐体二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入智能罐体二维码信息", notes = "导入智能罐体二维码信息")
    @PostMapping("/importSmartTanksChainnum")
    public R importSmartTanksChainnum(@RequestExcel List<ExcelImportTemplateVo.SmartTanksVo> excelVOList) {
        return codeExcelService.importSmartTanksChainnum(excelVOList);
    }

    @Inner(value = false)
    @ApiOperation(value = "获取打卡机列表", notes = "获取打卡机列表")
    @GetMapping("/getTimeClockList")
    public R<List<CodeTimeClock>> getTimeClockList() {
        return R.ok(codeTimeClockService.getTimeClockList());
    }

    /**
     * 导入回转窑二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入回转窑二维码信息", notes = "导入回转窑二维码信息")
    @PostMapping("/importRotaryKilnChainnum")
    public R importRotaryKilnChainnum(@RequestExcel List<ExcelImportTemplateVo.RotaryKilnVo> excelVOList) {
        return codeExcelService.importRotaryKilnChainnum(excelVOList);
    }

    /**
     * 导入法兰水表二维码信息
     *
     * @param excelVOList
     * @return
     */
    @ApiOperation(value = "导入法兰水表二维码信息", notes = "导入法兰水表二维码信息")
    @PostMapping("/importWaterMeterChainnum")
    public R importWaterMeterChainnum(@RequestExcel List<ExcelImportTemplateVo.WaterMeterVo> excelVOList) {
        return codeExcelService.importWaterMeterChainnum(excelVOList);
    }

    @ApiOperation(value = "导入语音合成播放二维码信息", notes = "导入语音合成播放二维码信息")
    @PostMapping("/importVoiceSynthesisChainnum")
    public R importVoiceSynthesisChainnum(@RequestExcel List<ExcelImportTemplateVo.VoiceSynthesisVo> excelVOList) {
        return codeExcelService.importVoiceSynthesisChainnum(excelVOList);
    }

    @ApiOperation(value = "导入声光报警器二维码信息", notes = "导入声光报警器二维码信息")
    @PostMapping("/importSoundLightAlarmChainnum")
    public R importSoundLightAlarmChainnum(@RequestExcel List<ExcelImportTemplateVo.SoundLightAlarmVo> excelVOList) {
        return codeExcelService.importSoundLightAlarmChainnum(excelVOList);
    }

}
